welcome back to data integration now you have learned how to collect the data from files from web from databases and from APIs now the next question will be how we can integrate those pieces of the dataset as one dataset so that we can continue to do data understanding data visualization data preprocessing and data analysis in this tutorial we are going to learn how to use the pandas built in method to merge or concatenate the different pieces together as one integrate dataset in order to do that you need to first import Numpy as np import pandas as pd we have done that many times the second thing is you need to import the dataset this time we are going to take the advantage of the built in dataset in Google Colaboratory so previously we have uploaded the dataset you download it from the module and upload them to here to play with them this time you just need to go to this file tab and click the sample data and then find either California Housing or California Housing training or testing either one so you can just choose one of them copy the path and paste it to this place this will help the Jupyter notebook environment where we can find the CSV file and in this case the CSV file is built in in Google Colaboratory Server once we read the CSV and assign it to df we can print out the df out so that we know we can have a big picture of the data frame in terms of the values and then we have longtitude and latitude of course we can also print out info that we can have the structure of this data frame where we have 3000 rows and we have 9 columns okay now we have the environment finished then let's continue with the concatenation we can find out further documentation through this link and again rely on those documentation is really a good habit because you can keep up to date and in order to show you how we can concatenate different pieces together in different ways we are going to create 3 subsets of this sample the 3 subsets is created in this way we are going to directly call from the data frame and slicing 3 labels or 3 columns from these 3 columns we are going to do a random sample to draw 5 rows out of this slicing basically df_1, as you can see here is a 5 row 3 column, a small data frame so as df_2,  5 rows 3 columns and df_3, 5 rows 3 columns and I made the 3 columns as exactly the same and the rows are different it's because I want to show you the concatenation direction may make a difference in df_cat1,  as we can see here we are going to call pd.concat() and assign all these 3 substats into this concatenation function this is indicating where we can find the dataference to be concatenated and then this axis equal to 0 will be you tell the concatenation the direction you are going to do in this case axis equal to 0 is we are going to match the indexes of the row label rather than the label of the columns as a result axis equal to 0 will be we have the index from df_1 an index from df_2 and also index from df_3 because our longitude latitude and medium income in all df1 2 3 are the same we can see that we can actually achieve the stacking or we can increment the observations of this concatenation by increment the rows the number of rows of the result so this is actually what we want because for example df_1 has the 1/3 of the observation df_2 have another 1/3 of observation etc so we are merging them in a way and making it a big data frame so this is the correct result however if you accidentally make the axis equal to one as you can see here we are still calling pd.concat() still feeling this 3 small data frame and here we are specifying axis equal to one and the result of the df_cat2 is actually surprising we actually have only df_1 in this place all the other 6 columns are missing we have df_2 in this place and all the other 2 square that is 5 rows and the 3 columns are missing and the df_3 is in this place but all the other columns are missing so this means that you are concatenating in a wrong direction or you are treating the latitudes in df_1 as a different latitude in df_2 as a different altitude in df_3 so if you are doing axis=1 concatenation the case is, for df_1 you have some attributes of the observation and for df_2 we have some other attributes of observation for df_3 we have another set of attributes and here we are going to concatenate for the first 5 rows we are going to select the longitude for the first 5 rows and the latitude for the first 5 rows and medium income again for the first 5 rows this is the case we have different data frame containing different attributes for the same observations let's look at how the axis is equal to one goes and now we can see if we do access equal to one we actually achieve the results that we have the 5 observations but we have 3 different attributes if you do actually equal to 0 that will basically means that you are increments the observation and then that will be resulting in something not you want for the merging I prepared several subsets of the data frame as well for df_1 we basically first the slicing longitude and the medium income and with only first 5 rows and for the df_2 we slice in the longitude and a medium house value again with the first 5 rows you can see here we have one longitude in common and they are all from the first 5 rows the merge will be looking like a SQL joining that we are going to call the pd.merge() and the first data frame, second data frame and this "on" specifies the foreign key or in other words this "on" specifies which attribute we are going to use for aligning this two data frame in this case, because we know the longitude is the shared attribute in df_1 and df_2 so we have the on as the longitude and the how is inner which we are going to specify later on and the result of this merge because as you can see here the longitude as the key for us to aligning these two dataframes can have the medium income from df_1 the medium house value from df_2 matched vertically nicely together in this merge the result here I'm going to show you how this inner is different than other ways so let's create another df_3 we still use the longitude as the key or as the common area and we have a population as another attribute this time we are going to use 2 to 7, which is 5 different rows this 5 different rows has some overlapping the df_1 was 0-4 and for this df_3 we have 2-6 basically we have 2, 3 and 4 shared in these 2 data frames let's see how the inner merge will look like the results on df_1 and  df_3 for the longitude and the inner merge will result in only the 3 common rows this 117 is actually index 2 and 118 is actually index 3, so on so forth and this result is again an automatically incremental integer as the index they are actually not the same as these 2 this 2 is not this 2, so don't be confused by that so this is the inner basically inner indicates this will be an intersection we are only going to keep the common rows for this merge how about we do outer for this outer, you can see that if we have an outer is more like a union that is we are going to have all the rows all the observations from the both data frame if we are missing something we just fiil the NaN so that we still keep all information without losing anything the drop duplicates is an optional call just in case when you do the merge when you do the concatenation when you do the joining you have resulted some duplicate rows then you can call this drop duplicates to remove them just for your information however in our tutorial there is no duplicates at all so again inner is to find out the common rows and outer is to find all the rows possible that is merge the next part is joining finally this will be the formal joining as we learned in SQL and the documentation is through this link to prepare for the joining now we are going to join based on certain things like in as our primary key or as our index, which is the label so we prepare the df_1 as one column longtitude with the first 5 rows and again df_2 has latitude another 5 rows the same as we just used before the first 5 rows is 0 to 4, and the second 5 rows is 2 to 6 let's see how the joining will happen and the joining is different than a merge the merge is called from pd, from pandas so that df_1 is in the same position as df_2 because the direction doesn't matter however for the joining, we are going to call from a data frame then you will have a direction depends on who you are going to call the joining and who you are going to feed into this joining function the data frame you call the joining will be defined as the left the data frame you feed in into the joining function will be regarded as the right so when we do the joining on df_2 by default it will be joining based on df_1's index if you call from df_1 and call the left basically you are going to have df1 as the priority result you are going to first lay out df_1 here and then fill as much information as possible from df_2 in this case because df_2 has the latitude of 2 3 and 4 so we fill these 3 rows and these 2 rows, because df_2 doesn't have that information so you result in a Nan the same logic happens to the right and now you can guess out if we have the right joining we are going to use df_2 has the priority result so you first lay out the latitude in df_2 and then fill as much information as possible from df1 so you can see that because df_1 has only 3 rows of the longitude then 5 and 6 will be missing value the joining can also apply with the inner and outer as well if we do the inner and outer basically the direction will not matter so df_1 joining with df_2 with the inner we are going to get the intersection of this two data frame and the outer will basically give you the union of this two data frame for the information we don't have, we are going to fill in the NaN so basically this is the simple tutorial of how we can merge how we can concatenate and how we can join 2 or multiple dataframes together